package com.application.lucky.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.application.lucky.Lucky;
import com.application.lucky.R;
import com.application.lucky.activity.MyInfoActivity;
import com.application.lucky.bean.UserBean;
import com.application.lucky.bean.sqliteBean.BankBean;
import com.application.lucky.gen.UserBeanDao;
import com.application.lucky.manager.UserManagers;
import com.application.lucky.request.CallBackEx;
import com.application.lucky.request.UserRequest;
import com.application.lucky.widget.TitlebarView;
import com.application.lucky.widget.dialog.TwoButtonDialog;
import com.application.mylibrary.base.BaseActivity;
import com.application.mylibrary.utils.EventBusUtils;
import com.application.mylibrary.utils.ToastUtils;
import com.baselib.libnetwork.net.libnetworkcomponent.BaseResponse;

import org.greenrobot.eventbus.Subscribe;

public class PerfectDataActivity extends BaseActivity implements View.OnClickListener {

    private TitlebarView mTitleBar;
    private RelativeLayout mRl1;
    private EditText mTvName;
    private ImageView mImg2;
    private EditText mTvPhoneNum;
    private RelativeLayout mRl4;
    private TextView mTvBank;
    private BankBean bean;
    private boolean isInput = true;
    private TwoButtonDialog mTwoButtonDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        EventBusUtils.register(PerfectDataActivity.this);
        initView();
    }

    @Override
    public void beforeInit() {

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_personal_basic;
    }

    private void initView() {
        mTitleBar = findViewById(R.id.title_bar);
        mTitleBar.setOnViewClick(new TitlebarView.onViewClick() {
            @Override
            public void leftClick() {
                finishAnim();
            }

            @Override
            public void rightClick() {
                if (isInput) {
                    mTwoButtonDialog = new TwoButtonDialog.Build(PerfectDataActivity.this)
                            .setMessage("提交后不可以修改！")
                            .setLeftButton("取消", null)
                            .setRightButton("确定", new TwoButtonDialog.BaseDialogClickListener() {
                                @Override
                                public void click() {
                                    commint();
                                }
                            }).create();
                    mTwoButtonDialog.show();
                }
            }
        });

        mRl1 = (RelativeLayout) findViewById(R.id.rl1);
        mTvName = (EditText) findViewById(R.id.tv_name);
        mImg2 = (ImageView) findViewById(R.id.img2);

        mTvPhoneNum = (EditText) findViewById(R.id.tv_phone_num);
        mRl4 = (RelativeLayout) findViewById(R.id.rl4);
        mTvBank = (TextView) findViewById(R.id.tv_bank);
        mRl1.setOnClickListener(this);
        mRl4.setOnClickListener(this);


        UserBean userBean = UserManagers.getInstance().getUserBean();
        if (userBean != null) {
            if (!TextUtils.isEmpty(userBean.getRealName())
                    && !TextUtils.isEmpty(userBean.getBankAddress())
                    && !TextUtils.isEmpty(userBean.getBankName())
                    && !TextUtils.isEmpty(userBean.getBank())
                    && !TextUtils.isEmpty(userBean.getMobile())) {
                isInput = false;
                bean = new BankBean();
                bean.name = userBean.getRealName();
                bean.accountAddress = userBean.getBankAddress();
                bean.accountNamw = userBean.getBankName();
                bean.num = userBean.getBank();
                mTvName.setText(getNameHideInfo(bean.name));
                mTvPhoneNum.setText(getPhoneHideInfo(userBean.getMobile()));
                mTvBank.setText("已绑定");
                mTvBank.setTag(true);
                mTitleBar.setRightText("");
            }
        }

        setState(isInput);
    }

    private void commint() {
        String name = mTvName.getText().toString();
        final String phoneNUm = mTvPhoneNum.getText().toString();

        if (TextUtils.isEmpty(name)) {
            ToastUtils.show("姓名不能为空！");
            return;
        }
        if (TextUtils.isEmpty(phoneNUm)) {
            ToastUtils.show("手机号不能为空！");
            return;
        }

        if (phoneNUm.length() != 11) {
            ToastUtils.show("手机号位数不对！");
            return;
        }

        if (bean == null) {
            ToastUtils.show("银行卡不能为空！");
            return;
        }
//        bean.name = name;
        new UserRequest().user_update_attr(bean.name, phoneNUm, new CallBackEx<BaseResponse<UserBean>>() {
            @Override
            public void onFailed(int code) {
                Toast.makeText(PerfectDataActivity.this, "提交信息失败,请重试", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onSuccess(BaseResponse<UserBean> stringBaseResponse) {
                UserBeanDao userBeanDao = Lucky.getApplication().getDaoSession().getUserBeanDao();
//                UserBean dataInfo = stringBaseResponse.dataInfo;
                if (userBeanDao != null) {
                    UserBean userBean = UserManagers.getInstance().getUserBean();
                    userBean.setBank(bean.num);
                    userBean.setRealName(bean.name);
                    userBean.setMobile(phoneNUm);
                    userBean.setBankAddress(bean.accountAddress);
                    userBean.setBankName(bean.accountNamw);
                    userBeanDao.insertOrReplace(userBean);
                    EventBusUtils.post(userBeanDao);
                }

                finishAnim();
            }
        });
    }


    public void setState(boolean is) {
        mTvName.setFocusable(is);
        mTvName.setFocusableInTouchMode(is);
        mTvPhoneNum.setFocusable(is);
        mTvPhoneNum.setFocusableInTouchMode(is);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl1:
                startActivityAnim(new Intent(this, MyInfoActivity.class));
                break;
            case R.id.rl4:
                if (TextUtils.isEmpty(mTvName.getText().toString())) {
                    ToastUtils.show("请先填入真实姓名");
                    return;
                }

                Intent intent = new Intent(this, SelectBankActivity.class);
                Object tag = mTvBank.getTag();
                boolean canUnbind = false;
                if (tag != null && tag instanceof Boolean) {
                    canUnbind = (Boolean) tag;
                }
                intent.putExtra("is", canUnbind);
                intent.putExtra("is_bind", true);
                intent.putExtra("auth_name", mTvName.getText().toString());
                if (bean != null) {
                    intent.putExtra("bean", bean);
                }
                startActivityForResult(intent, 1);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == 2) {
            if (data != null) {
                bean = data.getParcelableExtra("bean");
                mTvName.setText(bean.name);
                mTvBank.setText("已添加");
            }
        }
    }

    private String getNameHideInfo(String name) {
        return name.substring(0, 1) + "**";
    }

    // 13522768372
    private String getPhoneHideInfo(String phone) {
        if (TextUtils.isEmpty(phone) || phone.length() != 11) {
            return phone;
        }

        return phone.substring(0, 3) + "****" + phone.substring(8);
    }

    @Override
    protected void onDestroy() {
        if (mTwoButtonDialog != null && mTwoButtonDialog.isShowing()) {
            mTwoButtonDialog.dismiss();
        }
        EventBusUtils.unregister(PerfectDataActivity.this);
        super.onDestroy();
    }

    @Subscribe
    public void updateBindCard(BankBean item) {
        if (item == null) {
            return;
        }

        bean = item;
        mTvName.setText(getNameHideInfo(item.name));
        mTvBank.setText("已添加");
    }
}
